package jdl.tokenization.states;

import java.util.Hashtable;
import java.util.List;

import jdl.tokenization.Token;
import jdl.tokenization.TokenType;

public class WordState implements IState {
	private List<Token> tokens;
	private Hashtable<Character, TokenType> hashtable;
	private IState state;
	private String content = "";

	public WordState(List<Token> tokens,
			Hashtable<Character, TokenType> hashtable) {
		this.tokens = tokens;
		this.hashtable = hashtable;
		state = this;
	}

	@Override
	public void analyzeSymbol(char symbol) {
		if (symbol == ' ') {
			if (content.isEmpty()) {
				return;
			} else {
				tokens.add(new Token(content, TokenType.WORD));
				state = new InitialState(tokens, hashtable);
				return;
			}
		}
		TokenType tokenType = hashtable.get(symbol);
		if (tokenType != null && tokenType != TokenType.NUMBER) {
			if (!content.isEmpty()) {
				tokens.add(new Token(content, TokenType.WORD));
			}
			state = new InitialState(tokens, hashtable);
			state.analyzeSymbol(symbol);
		} else {
			if (content.isEmpty() && tokenType == TokenType.NUMBER) {
				state = new NumberState(tokens, hashtable);
				state.analyzeSymbol(symbol);
			} else {
				content += symbol;
			}
		}
	}

	@Override
	public IState getNextState() {
		return state;
	}

	@Override
	public void endOfSymbols() {
		tokens.add(new Token(content, TokenType.WORD));
	}
}
